package com.qch.rocketmq.springboot_ms_rocketmq.controller;

import com.qch.rocketmq.springboot_ms_rocketmq.pojo.GoodDO;
import com.qch.rocketmq.springboot_ms_rocketmq.pojo.OrderDO;
import com.qch.rocketmq.springboot_ms_rocketmq.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @Author qch
 * @Date 2021/6/23
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("/goods");
        List<GoodDO> list = goodsService.getAllList();
        view.addObject("userId", UUID.randomUUID().toString().replace("-",""));
        view.addObject("list", list);
        return view;
    }
    @RequestMapping("/seckill/{goodId}/{userId}")
    public ModelAndView pay(@PathVariable("goodId") String goodId,@PathVariable("userId") String userId ) {
        ModelAndView modelAndView=new ModelAndView("/wait.html");
        //生成主键订单id
        String orderId = UUID.randomUUID().toString().replace("-","");

        GoodDO goodDO = goodsService.seckill(Long.parseLong(goodId), userId, orderId);
        if(goodDO.getReserveNum()<=0)
        {
            modelAndView.setViewName("/nogoods.html");
        }else {
            modelAndView.addObject("userId",userId);
            modelAndView.addObject("orderId",orderId);
        }

        return modelAndView;
    }
}
